#!/bin/bash
# Filename:246Deploy
# Author:testdream
# Date:20160112

#脚本使用说明
#1、通过指定命令行的第1个参数以确定部署哪个服务，如部署全部则参数为all.
#all 全部更新
#更新250的媒体转换服务
#manager
#serveplatform
#poggyio-apn
#domainadmin



#host变量定义
remoteAddr=xxx.xxx.xxx.248
apiHost=xxx.xxx.xxx.247
managerHost=xxx.xxx.xxx.246
ImHost=xxx.xxx.xxx.250

#路径定义
deployfiles=/xxx/xxxx/autoDeploy
targetPath=/home/xxxx/test
managerPath=/xxxx/xxx/xxxx/apache-tomcat-8.0.28/webapps/workplus


#执行函数-参数1为部署目标
function deploy(){
#复制文件夹到本地
	if [ -z "$2" ];then
		echo "=========================deploy $1 service================================="
		echo "================================Delete files now!==============================="
		rm -rf $deployfiles/files/*$1*
		echo "================================Copy File Now!================================="
		scp -r -P 3701 atwork@$remoteAddr:$deployfiles/files/*$1*  $deployfiles/files/

			#echo "===============================BackFiles========================================"
		#rm -rf $targetPath/bakFiles/*$1*
		echo "=================================Deploy now!===================================="
		i=0
		#覆盖配置文件
		echo "==============================Update config file!==============================="
		cd $deployfiles/files
		for sinFile in `find ./ -type d  -name  "*$1*"`;do
		echo $sinFile
			if [ -d $sinFile ];then
				cp -rf $deployfiles/conf/$1/* $sinFile
				i=$[ $i+1 ]
			else
				rm -f $sinFile
			fi
		done
		#文件夹是否多于1个
		if [ $i -gt 1 ];then
			echo "=============Oops! The $1 folder more then one!Stop!===================="
			exit 1
		fi
			
			
		#备份删除服务
		echo "==========================Stop service and backup files!=========================="
		#pid_$1=`netstat -ntpl|grep 8083|awk -F[/] '{print $1}'|awk '{print $7}'`
		pid=`jps -v |grep $1|awk '{print $1}'`
		if [ -n "$pid" ];then
			kill -9 $pid
			echo "============The PID of $1 is deleted successfully!====================="
		fi
			
		#删除软链
		cd 	$targetPath
		rm $1
		#备份并更新名称
		i=0
		for sinFold in `find ./ -type d  -name  "*$1*"`;do
		echo $sinFold
			if [ -d $sinFold ];then
				mv $targetPath/*$1*  $targetPath/bakFiles/$(basename $sinFold)-$(date +%y%m%d%H%M%S)		
				i=$[ $i+1 ]
			else
				rm -f $sinFold
			fi
		done
		#文件夹是否多于1个
		if [ $i -gt 1 ];then
			echo "=============Oops! The $1 folder more then one!Stop!===================="
		fi
		
		rm -rf $targetPath/*$1*
			
		#复制目标文件夹
		cp -r $deployfiles/files/*$1*  $targetPath/
			
		#启动应用
		echo "==========================Add soft link and start service!========================="
		ln -s $targetPath/*$1*  $targetPath/$1
		if [ -n "$pid" ];then
			j=0
			while(($j<60))
			do
				port=`netstat -ntpl|grep $pid |awk '{print $1}'`
				if [ -z "$port" ];then
					
					break
				fi
				sleep 2
				j=$[ $j+1 ]
			done
		fi
	fi
	sh  $targetPath/$1/bin/start.sh

}



#246服务器部署

case $1 in 
	poggyio-apn|all)
		deploy poggyio-apn
	;;&
	domainadmin|all)
		deploy domainadmin
	;;&
	serveplatform|all)
		deploy serveplatform
	;;&
	manager|all)
	
		#复制文件夹到本地
		
		if [ -z "$2" ];then
			
			echo "=========================deploy manager service================================="
			echo "================================Delete files now!==============================="
			rm -rf $deployfiles/files/*manager*
			echo "================================Copy File Now!================================="
			scp -rP 3701  atwork@$remoteAddr:$deployfiles/files/*manager*  $deployfiles/files/

			echo "===============================BackFiles========================================"
			rm -rf $managerPath/../../bakFiles/manager/*
			echo "=================================Deploy now!===================================="
			i=0
			#覆盖配置文件
			echo "==============================Update config file!==============================="
			cd $deployfiles/files
			for sinFile in `find ./ -type d  -name  "*manager*"`;do
			echo $sinFile
				if [ -d $sinFile ];then
					cp -f $deployfiles/conf/manager/application.properties $sinFile/WEB-INF/classes
                                        cp -f $deployfiles/conf/manager/quartz.properties $sinFile/WEB-INF/classes
					cp -f $deployfiles/conf/manager/config.json $sinFile/services/ajax/jsp
					i=$[ $i+1 ]
				else
					rm -f $sinFile
				fi
			done
			#文件夹是否多于1个
			if [ $i -gt 1 ];then
				echo "=============Oops! update config!sThe manager folder more then one!Stop!===================="
				exit 1
			fi
			
			
			#备份删除服务
			
			echo "==========================Stop service and delete files!=========================="
			#pid_manager=`netstat -ntpl|grep 8083|awk -F[/] '{print $1}'|awk '{print $7}'`
			pid_manager=`jps -v |grep tomcat|awk '{print $1}'`
			if [ -n "$pid_manager" ];then
				kill -9 $pid_manager
				echo "============The PID of manager is deleted successfully!====================="
			fi
			
			cp -r  $managerPath  $managerPath/../../bakFiles/manager-$(date +%y%m%d%H%M%S)	
			rm -rf $managerPath/*
			
			#复制目标文件夹
			cp -r $deployfiles/files/manager/*  $managerPath/
			
			#启动应用
			echo "==========================start service!========================="
			if [ -n "$pid_manager" ];then
				j=0
				while(($j<60))
				do
					port=`netstat -ntpl|grep $pid_manager |awk '{print $1}'`
					if [ -z "$port" ];then
						
						break
					fi
					sleep 2
					j=$[ $j+1 ]
				done
			fi
		fi
		sh  $managerPath/../../bin/startup.sh
	;;&
esac

exit 0 
